package sk.marek.barak.client;

import java.util.List;
import java.util.Date;

import sk.marek.barak.shared.ComentServiceFactory;
import sk.marek.barak.shared.ThoughtServiceFactory;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DateLabel;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.VerticalPanel;


public class ThoughtUI extends Composite {
	private String name;
	private Date date;
	private String text;
	private String userId;
	private String thougtKey;
	private DecoratedPopupPanel popup;
	private ComentServiceAsync comentService;
	Label label;
	private Button btnComents;
	
	public ThoughtUI(UserThought thg) {
		this.name = thg.getUser();
		this.text = thg.getText();
		this.date = thg.getDate();
		this.thougtKey = thg.getThougKey();
		this.userId = thg.getUserId();
		
		DockPanel mainPanel = new DockPanel();
		initWidget(mainPanel);
		mainPanel.setSize("285px", "142px");
		DateLabel dateLabel = new DateLabel();
		dateLabel.setStyleName("gwt-Label");
		dateLabel.setValue(this.date);
		
		label = new Label(this.name);
		AbsolutePanel northPanel = new AbsolutePanel();
		northPanel.setSize("285px","23px");
		northPanel.add(label,0,0);
		northPanel.add(dateLabel,180,0);
		dateLabel.setSize("82px", "23px");
		mainPanel.add(northPanel,DockPanel.NORTH);
		
		label.setSize("159px", "23px");
		
		PushButton pshbtnRemove = new PushButton("x");
		northPanel.add(pshbtnRemove, 266, 0);
		pshbtnRemove.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(ShareLife.getCurentUser().getUserID()==userId){
					if(comentService==null)
						comentService = ComentServiceFactory.getComentService();
				
					comentService.removeThoughtComents(thougtKey, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Failed to remove coment");
						
						}

						@Override
						public void onSuccess(Void result) {
						
						
						}
			
					});
					ThoughtServiceFactory.getThoughtService().deleteThought(thougtKey, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Failded to remove thought");
						
						}

						@Override
						public void onSuccess(Void result) {
							// TODO Auto-generated method stub
						
						}
					
				});
				
			removeFromParent();	
			
			}else{
				Window.alert("You can delete just your own thoughts");
			}
		}
			
		});
		
		TextArea textArea = new TextArea();
		textArea.setReadOnly(true);
		textArea.setText(this.text);
		
		mainPanel.add(textArea, DockPanel.NORTH);
		textArea.setSize("275px", "78px");
		
		btnComents= new Button("Coments");
		mainPanel.add(btnComents, DockPanel.SOUTH);
		btnComents.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if (popup==null)
					setPoup();
				relockPopup();
				popup.show();
			}
		});
		
		
	Window.addResizeHandler(new ResizeHandler() {
		
		@Override
		public void onResize(ResizeEvent event) {
			relockPopup();

			
		}
	});
	}
	private void setPoup(){
		
		popup =new DecoratedPopupPanel();
		popup.setStyleName("popupPanel");
		
		VerticalPanel mainPanel = new VerticalPanel();
		final FlexTable coments= new FlexTable();
		mainPanel.add(coments);
		
		PushButton close = new PushButton("x");
		TextArea text = new TextArea();
		text.setReadOnly(true);
		text.setText(this.text);
		text.setStyleName("userProfile");
		coments.setWidget(0, 0, text);
		coments.setWidget(0, 2,close);
		coments.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
			popup.hide();
				
			}
		});
		
		if(comentService==null) 
			comentService = ComentServiceFactory.getComentService();
		comentService.getThoughtComents(thougtKey,new AsyncCallback<List<Coment>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<Coment> result) {
				for(Coment coment : result){
					coments.setWidget(coments.getRowCount(),0, new ComentUI(coment));
				}
				
			}
		
		
		
		});
		
		
		HorizontalPanel downPanel = new HorizontalPanel();
		final TextArea coment = new TextArea();
		PushButton addComent = new PushButton("coment");
		addComent.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
			if (!coment.getText().equals("")){
				TextArea text = new TextArea();
				text.setText(coment.getText());
				text.setReadOnly(true);

				if(comentService==null)
				comentService = ComentServiceFactory.getComentService();
				
				comentService.addComent(text.getText(),thougtKey,new AsyncCallback<Coment>() {
					
					@Override
					public void onSuccess(Coment result) {
						coments.setWidget(coments.getRowCount(), 0, new ComentUI(result));
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Failed to post coment");
					}
				});
				coment.setText("");
			};
				
			}
		});
		
		downPanel.add(coment);
		downPanel.add(addComent);
		mainPanel.add(downPanel);
		ScrollPanel mainScrollPanel = new ScrollPanel();
		mainScrollPanel.addScrollHandler(new ScrollHandler() {
			
			@Override
			public void onScroll(ScrollEvent event) {
			relockPopup();
				
			}
		});
		mainScrollPanel.add(mainPanel);
		mainScrollPanel.setHeight("300px");
		mainScrollPanel.setWidth("260px");
		mainScrollPanel.setStyleName("comentField");
		popup.setWidget(mainScrollPanel);
		popup.setAnimationEnabled(true);
	}
	public void relockPopup(){
		if(popup!=null)
		popup.setPopupPosition(label.getAbsoluteLeft(),label.getAbsoluteTop());
	}
	
	public void closePopup(){
		popup.hide();
	}
	@Override
	protected void onUnload() {
		if(popup!=null)
		popup.hide();
		
		super.onUnload();
	}
}
